package com.wisdudu.blank.ui.common;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.orhanobut.logger.Logger;
import com.wisdudu.blank.R;
import com.wisdudu.blank.databinding.FragmentSelectSystemImgBinding;
import com.wisdudu.frame.base.BaseFragment;
import com.wisdudu.blank.support.rxbus.RxBusFlag;

/**
 * Created by qyf on 2016/7/11.
 */
public class SelectSystemImgFragment extends BaseFragment {
    public static final int REQUEST_SYSTEM_IMG = 101;

    @Override
    protected View initBinding(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        FragmentSelectSystemImgBinding mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_select_system_img, container, false);
        mBinding.setViewModel(new SelectSystemImgVM(this));
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initToolbar(getToolbar(), "系统图片");
    }

    //    private void initLoading() {
//        mFlLoad.setOnLoadClickListener(new LoadingFrameLayout.OnLoadRetryClickListener() {
//            @Override
//            public void onErrorRetryClick(View view) {
//                getPresenter().initData();
//            }
//
//            @Override
//            public void onEmptyRetryClick(View view) {
//                getPresenter().initData();
//
//            }
//        });
//
//    }
//
//    public LoadingFrameLayout getFlLoad() {
//        return mFlLoad;
//    }
//
////    public LoadingManager getLoadingManager() {
////        return mLoadingManager;
////    }
//
//    public RecyclerView getRecycler() {
//        return mRecycler;
//    }
//
//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        ButterKnife.unbind(this);
//    }
//
//    @OnClick(R.id.iv_back)
//    public void onClick() {
//        getFragmentManager().popBackStack();
//    }
//
//    public class GroupAdapter extends BaseQuickAdapter<SystemImg> {
//
//        public GroupAdapter(List<SystemImg> data) {
//            super(R.layout.item_system_img_group, data);
//        }
//
//        @Override
//        protected void convert(BaseViewHolder baseViewHolder, SystemImg systemImg) {
//            baseViewHolder.setText(R.id.group_title, systemImg.getGroupname());
//            imgAdapter imgAdapter = new imgAdapter(systemImg.getPhoto());
//            imgAdapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
//                @Override
//                public void onItemClick(View view, int i) {
//                    Log.i(TAG, "onItemClick: 点击：" + i);
//                    Intent intent = new Intent(getContext(), ImageGlanceActivity.class);
//                    ArrayList<String> pahts = new ArrayList<>();
//                    for (SystemImg.PhotoEntity photoEntity : systemImg.getPhoto()) {
//                        pahts.add(photoEntity.getOriginalimg());
//                    }
//                    intent.putStringArrayListExtra(RxBusFlag.IMAGE_GLANCE_PATHS, pahts);
//                    intent.putExtra(RxBusFlag.CURRENT_PHOTO_INDEX, i);
//                    intent.putExtra(RxBusFlag.IS_SHOW_SAVE, true);
//                    startActivityForResult(intent, REQUEST_SYSTEM_IMG);
//                }
//            });
//            RecyclerView imgRecycler = baseViewHolder.getView(R.id.recycler_img);
//            imgRecycler.setLayoutManager(new GridLayoutManager(getContext(), 3));
//            (imgRecycler).setAdapter(imgAdapter);
//        }
//    }

    //    public class imgAdapter extends BaseQuickAdapter<SystemImg.PhotoEntity> {
//
//        public imgAdapter(List<SystemImg.PhotoEntity> data) {
//            super(R.layout.item_system_img, data);
//        }
//
//        @Override
//        protected void convert(BaseViewHolder baseViewHolder, SystemImg.PhotoEntity photoEntity) {
//            Glide.with(getContext()).load(photoEntity.getOriginalimg()).into((ImageView) baseViewHolder.getView(R.id.iv_system));
//        }
//    }
//
//    public void showSystemImgs(List<SystemImg> systemImgs) {
//        mRecycler.setAdapter(new GroupAdapter(systemImgs));
//    }
//
//    private static final String TAG = "SelectSystemImgFragment";
//
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_SYSTEM_IMG) {
            if (resultCode == Activity.RESULT_OK) {
                Logger.i("onActivityResult:从Glance中取回照片路径为：%s ", data.getStringArrayListExtra(RxBusFlag.SELECT_IMGS));
                getActivity().setResult(Activity.RESULT_OK, data);
                getActivity().finish();
            }
        }

    }
}
